﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TravelingWebApp.Models;
using TravelingWebApp.Data;
using System.IO;

namespace TravelingWebApp.Repositories
{
    public class ScenaryRepositories : IScenaryRepositories
    {
        private TravelingContext _ctx;

        public ScenaryRepositories(TravelingContext ctx)
        {
            _ctx = ctx;
        }

        public IEnumerable<Scenary> GetScenaries()
        {
            return _ctx.DbScenaries.ToList();
        }

        public Scenary GetScenaryById(string id)
        {
            return _ctx.DbScenaries.Include(b => b.Id)
                .SingleOrDefault(c => c.Id == id);
        }

        public void AddScenary(Scenary scenary)
        {
            //if (scenary.PhotoAvatar != null && scenary.PhotoAvatar.Length > 0)
            //{
            //    scenary.ImageMimeType = scenary.PhotoAvatar.ContentType;
            //    scenary.ImageName = Path.GetFileName(scenary.PhotoAvatar.FileName);
            //    using (var memoryStream = new MemoryStream())
            //    {
            //        scenary.PhotoAvatar.CopyTo(memoryStream);
            //        scenary.PhotoFile = memoryStream.ToArray();
            //    }
            //}
            _ctx.Add(scenary);
            _ctx.SaveChanges();
        }

        public void DeleteScenaryById(string id)
        {
            var scenary = _ctx.DbScenaries.SingleOrDefault(c => c.Id == id);
            _ctx.DbScenaries.Remove(scenary);
            _ctx.SaveChanges();
        }

        public void SaveChanges()
        {
            _ctx.SaveChanges();
        }

    }
}
